package com.intellij.licensecommon.net.impl;

import com.intellij.licensecommon.logging.Logger;
import com.intellij.licensecommon.net.Address;
import com.intellij.licensecommon.net.NetworkSocket;
import com.intellij.licensecommon.net.h;
import com.intellij.licensecommon.net.i;

import java.io.IOException;
import java.net.*;

public class SocketImpl implements NetworkSocket {
    private static final Logger a = a.a.a("#com.intellij.licensecommon.net.impl.SocketImpl");
    private final DatagramSocket datagramSocket;
    private boolean c;
    private final Object LOCK = new Object();
    public static int e = 10000;

    public SocketImpl(DatagramSocket ds) {
        this.datagramSocket = ds;
    }

    public SocketImpl(DatagramSocket socket, int soTimeout) {
        this(socket);
        try {
            this.datagramSocket.setSoTimeout(soTimeout);
        } catch (SocketException localSocketException) {
        }
    }

    public void a(i parami) {
        b localb = (b) parami.a();
        byte[] arrayOfByte = parami.b();
        InetSocketAddress localInetSocketAddress = localb.getStocketAddress();
        DatagramPacket localDatagramPacket = null;
        try {
            localDatagramPacket = new DatagramPacket(arrayOfByte, arrayOfByte.length, localInetSocketAddress);
        } catch (Exception localException) {
            if (a.isDebugEnabled()) {
                a.debug(localException);
            }
        }
        if (localDatagramPacket == null) {
            return;
        }
        try {
            this.datagramSocket.send(localDatagramPacket);
        } catch (IOException localIOException) {
            if (a.isDebugEnabled()) {
                a.debug(localIOException);
            }
        }
    }

    public i a() throws h, com.intellij.licensecommon.net.e {
        synchronized (this.LOCK) {
            this.c = true;
            this.LOCK.notifyAll();
        }
        ???=new byte[10000];
        DatagramPacket localDatagramPacket = new DatagramPacket((byte[])???,???.length);
        try {
            this.datagramSocket.receive(localDatagramPacket);
        } catch (SocketTimeoutException localSocketTimeoutException) {
            throw new f(localSocketTimeoutException);
        } catch (SocketException localSocketException) {
            throw new h(localSocketException);
        } catch (IOException localIOException) {
            throw new com.intellij.licensecommon.net.e(localIOException);
        }
        byte[] arrayOfByte = new byte[localDatagramPacket.getLength()];
        System.arraycopy(localDatagramPacket.getData(), localDatagramPacket.getOffset(), arrayOfByte, 0, localDatagramPacket.getLength());
        return new i(arrayOfByte, new b(localDatagramPacket.getSocketAddress()));
    }

    public void b() {
        this.datagramSocket.close();
    }

    public void c() throws InterruptedException {
        synchronized (this.LOCK) {
            if (this.c) {
                return;
            }
            this.LOCK.wait();
        }
    }

    public boolean d() {
        synchronized (this.LOCK) {
            return this.c;
        }
    }

    public Address e() {
        return new b(this.datagramSocket.getLocalSocketAddress());
    }
}
